import json
import logging
import time
import requests

from log.log_tools import logger
from tools import UA_pools
from tools import ip_proxies

BASE_URL = 'https://linnean-online.org/'
ua = UA_pools.Get_ua()
ip = ip_proxies.Get_IP()


# def get_index(*params):
#     # i = 0
#     # while i < 10:
#     #     try:
#     #         response = requests.get(url=params[0], headers=params[1],params=params[2],proxies=ip.get_proxy(),timeout=20,)
#     #         return response
#     #         # time.sleep(1)
#     #     except requests.exceptions.RequestException as e:
#     #         i+=1
#     #         # time.sleep(1)
#     #         print(e)
#     #         logging.log(80,'发生异常:' + str(e))
#     #     #     log_main()
#     session = requests.Session()
#     res = session.get(BASE_URL)
#     # 设置最大重连次数
#     session.mount('http://', HTTPAdapter(max_retries=20))
#     session.mount('https://', HTTPAdapter(max_retries=20))
#     session.keep_alive = False
#     try:
#         proxies = ip.get_proxy()
#         session.proxies = proxies
#         response = session.get(url=params[0], headers=params[1],params=params[2],timeout=20)
#         return response
#
#     except Exception as e:
#         logging.log(80,'发生异常:' + str(e))


def get_index(*params):
    i = 0
    while i < 10:
        try:
            # proxies = ip.get_proxy()
            # str_proxies = json.dumps(proxies)
            response = requests.get(url=params[0], headers=params[1], params=params[2])
            time.sleep(5)
            return response

        except Exception as e:
            i += 1
            logger.error(params[0])
            logger.error(e)
            time.sleep(5)
